import { defineConfig, loadEnv } from 'vite';
import createVitePlugins from './vite/plugins';
import { resolve } from 'path';

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
    const env = loadEnv(mode, process.cwd());
    const { VITE_APP_ENV } = env;
    return {
        plugins: createVitePlugins(env, command === 'build'),
        build: {
            emptyOutDir: true,
            //打包文件目录
            outDir: 'es',
            //压缩
            // minify: 'terser',
            rollupOptions: {
                //忽略打包vue文件
                external: ['vue','element-plus', "dayjs","radash","lodash-es", '@element-plus/icons-vue'],
                input: ['./packages/index.ts'],
                output: [
                     {
                        //打包格式
                        format: 'umd',
                        //打包后文件名
                        entryFileNames: '[name].js',
                        //让打包目录和我们目录对应
                        // preserveModules: true,
                        // exports: 'default',
                        //配置打包根目录
                        dir: './ubasic-ui/dist',
                        name: 'UBasicUI'
                    },
                    {
                        //打包格式
                        format: 'es',
                        //打包后文件名
                        entryFileNames: '[name].mjs',
                        //让打包目录和我们目录对应
                        preserveModules: true,
                        exports: 'named',
                        //配置打包根目录
                        dir: './ubasic-ui/es',
                    },
                    {
                        //打包格式
                        format: 'cjs',
                        //打包后文件名
                        entryFileNames: '[name].js',
                        //让打包目录和我们目录对应
                        preserveModules: true,
                        exports: 'named',
                        //配置打包根目录
                        dir: './ubasic-ui/lib',
                    },
                   
                ],
            },
            lib: {
                entry: resolve(__dirname, 'packages', 'index.ts'),
                formats: ['es', 'cjs', 'umd'],
                name: 'UBasicUI',
                fileName: 'ubasic-ui',
            },
        },
    };
});
